package com.rachel.materialdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.rachel.materialdemo.ItemClickListener;
import com.rachel.materialdemo.R;
import com.rachel.materialdemo.ToolUtils;
import com.rachel.materialdemo.adapter.DemoRecyclerviewAdapter;
import com.rachel.materialdemo.bean.Cardone;
import com.rachel.materialdemo.bean.Cardtwo;
import com.rachel.materialdemo.bean.ParentBean;
import com.rachel.materialdemo.bean.ViewBean;

import java.util.ArrayList;
import java.util.List;

public class NavigationViewActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener,
        View.OnClickListener, ItemClickListener {
    private static final String TAG = "zsr";
    /**
     * ui
     */
    private DrawerLayout mDrawerLayout;
    private RecyclerView mRecyclerView;
    private DemoRecyclerviewAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.menu);
        }

        FloatingActionButton ball = (FloatingActionButton) findViewById(R.id.float_tick);
        ball.setOnClickListener(this);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setItemIconTintList(null); //设置成null，保证你图标原有的颜色，否则根据系统颜色设定
        navigationView.setNavigationItemSelectedListener(this);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,6);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewtype = mRecyclerView.getAdapter().getItemViewType(position);
                switch (viewtype){
                    case ToolUtils.TYPE_VIEWPAGER:
                        return 6;
                    case ToolUtils.TYPE_ONE:
                        return 3;
                    case ToolUtils.TYPE_TWO:
                        return 2;
                }
                return 0;
            }
        });
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new DemoRecyclerviewAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setonItemClickListener(this);

        mRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swip_refresh);
        //设置下拉转圈的颜色
        mRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
        //下拉监听
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshDatas();
            }


        });


        //mViewPager = (ViewPager) findViewById(R.id.viewpager);

        initCardData();
       // mViewPager.setAdapter(new BannerAdapter(mViews));

    }
    List<View> mViews = new ArrayList<>();

    /**
     * 初始化数据
     */
    private void initCardData() {
        List<Cardone> cardones = new ArrayList<>();
        List<Cardtwo> cardtwos = new ArrayList<>();
        List<ViewBean> viewBeens = new ArrayList<>();
        
        List<ParentBean> mParentBeen = new ArrayList<>();
        mViews.clear();
        for (int i = 0; i < 1; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.viewpager_item,null);
            ViewBean bean = new ViewBean();
            bean.setViewtype(ToolUtils.TYPE_VIEWPAGER);
            bean.setView(view);
            viewBeens.add(bean);
        }

        for (int i = 0; i < 4; i++) {
            Cardone bean =  new Cardone();
            bean.setViewtype(ToolUtils.TYPE_ONE);
            bean.setResid(R.mipmap.beauty);
            bean.setText("测试："+i);
            cardones.add(bean);
        }
        for (int i = 0; i < 9; i++) {
            Cardtwo cardtwo = new Cardtwo();
            cardtwo.setViewtype(ToolUtils.TYPE_TWO);;
            cardtwo.setResid(R.mipmap.image5);
            cardtwos.add(cardtwo);
        }

        mParentBeen.addAll(cardones);
        mParentBeen.addAll(cardtwos);

        for (ParentBean parentBean : mParentBeen) {
            Log.d(TAG, "initCardData: "+parentBean.getViewtype()+" ");
        }

        mAdapter.setDatas(viewBeens,cardones,cardtwos);
        mAdapter.notifyDataSetChanged();

    }
    
    private <T> void  addLallist(List<T> data){

    }




    /**
     * 刷新数据
     */
    private void refreshDatas() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //用一个延时来模拟网络刷新
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initCardData();
                        mRefreshLayout.setRefreshing(false);//刷新结束
                    }
                });
            }
        }).start();
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.nav_call:
                Toast.makeText(NavigationViewActivity.this, "点击了 Call", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_friends:
                Toast.makeText(NavigationViewActivity.this, "点击了 Frirends", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_location:
                Toast.makeText(NavigationViewActivity.this, "点击了 Location", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_mail:
                Toast.makeText(NavigationViewActivity.this, "点击了 Mail", Toast.LENGTH_SHORT).show();

                break;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.float_tick) {
            Snackbar.make(v, "data ball ", Snackbar.LENGTH_SHORT)
                    .setAction("nodo", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(NavigationViewActivity.this, "hello world", Toast.LENGTH_SHORT).show();
                        }
                    }).show();
        }
    }


    @Override
    public void onItemClickListener(View view, int position) {
        int imgresid = R.mipmap.beauty;
        String text = "beatiful girl";
        Intent intent = new Intent(this,TestDetailActivity.class);
        intent.putExtra("imgid",imgresid);
        intent.putExtra("string",text);
        startActivity(intent);

    }
}
